<?php

namespace app\common\service\user;


use app\admin\model\freight\ExpressCity;
use app\admin\model\UserAddress;
use app\common\service\BaseService;

class AddressService extends BaseService
{

    /**
     * Author:Yingbo Sun
     * 添加地址
     * @param $params
     * @return array
     */
    public static function addSite($params)
    {
        $data = UserAddress::create($params);
        if ($data) {
            return self::success('添加成功');
        } else {
            return self::fail('添加失败');
        }
    }

    /**
     * Author:Yingbo Sun
     * 修改默认地址
     * @param $uid
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException\
     */
    public static function defaultSite($uid, $id)
    {
        $where[] = ['id', '=', $id];
        $where[] = ['uid', '=', $uid];
        $data = UserAddress::where($where)->find();
        $data->is_default = 0;
        $wheres[] = ['is_default', '=', 0];
        $wheres[] = ['uid', '=', $uid];
        $res = UserAddress::where($where)->find();
        $res->is_default = 1;
        $data = $data->save();
        $res = $res->save();
        if ($data && $res) {
            return self::success('修改成功');
        } else {
            return self::fail('修改失败');
        }
    }


    /**
     * Author:YangXinPeng
     * 根据用户地址查询对于的地址
     * @param $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function show($uid)
    {
        $data = UserAddress::where('uid', $uid)->select();
        $data && $data = $data->toArray();
        return self::success('成功', $data);
    }

    /**
     * Author:YangXinPeng
     * 根据ID查询
     * @param $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findById($uid)
    {
        $data = UserAddress::find($uid);
        $data && $data = $data->toArray();
        return self::success('成功', $data);
    }


    /**
     * Author:YangXinPeng
     * 查询三级联动合并
     * @param $eid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function one($eid)
    {
        $value = UserAddress::find($eid);
        $value && $value = $value->toArray();
        $province = ExpressCity::where('id', $value['province_region'])->value('city_name');
        $city = ExpressCity::where('id', $value['city_region'])->value('city_name');
        $district = ExpressCity::where('id', $value['district_region'])->value('city_name');
        $value['address'] = $province . $city . $district.$value['detailed_address'];
        return self::success('成功',$value);
    }


    public static function addressOne($aid)
    {
        if (empty($aid)){
            return self::fail('参数错误');
        }
        $data = UserAddress::find($aid);
        $data && $data = $data->toArray();
        $province = ExpressCity::where('id', $data['province_region'])->value('city_name');
        $city = ExpressCity::where('id', $data['city_region'])->value('city_name');
        $district = ExpressCity::where('id', $data['district_region'])->value('city_name');
        $value['address'] = $province . $city . $district.$data['detailed_address'];
        $value['provinces'] = $province . $city . $district;
        return  self::success('获取成功',$value);

    }

}